/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewFilterDialog.java
 *
 * Created on 2.5.2011, 16:52:13
 */
package cz.muni.fi.pb138.deskal.gui;

import cz.muni.fi.pb138.deskal.CalendarManager;
import cz.muni.fi.pb138.deskal.Filter;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Drak
 */
public class NewFilterDialog extends javax.swing.JDialog {

    private Filter filter = new Filter();
    private JList list;
    private FiltersListModel model;
    private CalendarManager calManager;
    private AddFilterWorker addFilterWorker;

    private class AddFilterWorker extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            calManager.addFilter(filter);
            return null;
        }

        protected void done() {
            model.addFilter(filter);
        }
    }

    /** Creates new form NewFilterDialog */
    public NewFilterDialog(java.awt.Frame parent, boolean modal, JList filtersList,
            CalendarManager calManager) {
        super(parent, modal);
        initComponents();
        this.calManager = calManager;
        list = filtersList;
        model = (FiltersListModel) list.getModel();
        filterText.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        filterText = new javax.swing.JTextField();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("DesKal");
        setResizable(false);

        jLabel1.setText("Nový štítek:");

        filterText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterTextActionPerformed(evt);
            }
        });

        addButton.setText("Přidat");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Zpět");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filterText, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(124, Short.MAX_VALUE)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filterText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(addButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                addAndDispose();
            }
        });


    }//GEN-LAST:event_addButtonActionPerformed

    private void filterTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterTextActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                addAndDispose();
            }
        });
    }//GEN-LAST:event_filterTextActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField filterText;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    private void addAndDispose() {
        String name = filterText.getText();
        if (!name.trim().equals("")) {
            filter.setName(name);
            if (!model.containsFilter(filter)) {
                addFilterWorker = new AddFilterWorker();
                addFilterWorker.execute();
                dispose();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Štítek " + name + " již existuje", "DesKal", JOptionPane.ERROR_MESSAGE);
                filterText.requestFocus();
            }
        } else {
            filterText.requestFocus();
        }
    }
}
